package com.google.code.cakedroid.context;
//import necessary packages
import java.io.File;

import com.google.code.cakedroid.base.BaseContext;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;

/**
 *APK  Helper Class 
 *Currently,You can use the methods to install an apk file and uninstall an application
 *@author androidyue
 *Last Modified:2011-12-8    pm 12:55:24
 */
public class APKHelper extends BaseContext {
	private  final String   APK_DATA_TYPE="application/vnd.android.package-archive";
	
	/**
	 * constructor It's recommended to call this constructor the instantiate an object.
	 * @param context  Context object
	 */
	public APKHelper(Context context){
		this.context=context;
	}
	
	
	/**
	 * Install apk from a file
	 * @param file  destination apk file
	 */
	public void installAPK(File file){
		Uri uri=Uri.fromFile(file);
		Intent intent=new Intent();
		intent.setDataAndType(uri,this.APK_DATA_TYPE );
		this.installAPK(intent);
	}
	
	
	/**
	 * install apk from an intent
	 * @param intent  the wanted intent
	 */
	public void installAPK(Intent intent){
		intent.setAction(Intent.ACTION_VIEW);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		this.context.startActivity(intent);
	}
	
	
	/**
	 * uninstall apk file
	 * @param packageName  the starter class name of an application
	 */
	public void uninstallAPK(String packageName){
		//somehow it's  like "package:ps.androidyue.google_service_starter.GoogleServiceStartActivity"
		Uri uri=Uri.parse("package:"+packageName);
		Intent intent=new Intent(Intent.ACTION_DELETE,uri);
		this.context.startActivity(intent);
	}
	
}
